<?php
if(!defined("PAGE_ENABLE")) exit;
/**
 * @author Josef Fröhle
 * @copyright 2010
 */

class Controller {
    
    private $data = array();

    function __construct() {
        global $mysqli;
        
        $this->checkMaintaice();
        
        $this->ClassName = preg_replace("@_Controller@",'',get_class($this));
        
        /** Start Smarty Class */
        require_once(SMARTYDIR."/Smarty.class.php");
        $this->smarty = new Smarty;
        $this->smarty->template_dir = SMARTY_HTML;
        $this->smarty->compile_dir = SMARTY_CHTML;
        $this->smarty->config_dir = SMARTY_CONFIG;
        $this->smarty->cache_dir = SMARTY_CACHE;
        $this->smarty->allow_php_tag = true;
        $this->smarty->force_compile = true;
        $this->smarty->debugging = false;
        
        /** Start MySQLi Class */
        //TODO:Hier MySQLi einbauen
        $this->db = $mysqli;
        if ($this->db->connect_errno) {
            PageError::show503(600);
        }
        
        #echo strlen(skein_hash_hex("",1024));

    }
    
    
    /** MAGIC-DATA GETTING */
    
    public function __set($name, $value) {
        $this->data[$name] = $value;
    }

    public function __get($name) {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }

        $trace = debug_backtrace();
        trigger_error(
            'Undefined property via __get(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }

    public function __isset($name) {
        return isset($this->data[$name]);
    }

    public function __unset($name) {
        unset($this->data[$name]);
    } 
    
    public function smarty_view($name) {
        if (isset($this->error)){
            $this->smarty->assign('error', $this->error);
        }        
        $this->smarty->display($name);
    }
    
    public function isLoadClass($class_name) {
        return class_exists($class_name,false);
    }
    
    public function smarty_set($name,$value) {
        $this->smarty->assign($name,$value);
    }
    
    // Wird auf eine SSL-Seite weiter geleitet.
    function goSSL(){
        if($_SERVER["HTTP_HTTPS"] == "on"){
            $checkResult = TRUE;
        } elseif ($_SERVER['HTTP_X_PORT'] == "443") {
            $checkResult = TRUE;
        } else {
            $checkResult = FALSE;
        }
        
        if ($checkResult === FALSE) {
            header("Location: https://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
            exit; 
        } else {
            return $checkResult;
        }
    } 
    
    // Wird auf eine SSL-Seite weiter geleitet.
    function goNoneSSL(){
        if($_SERVER["HTTP_HTTPS"] == "on"){
            $checkResult = TRUE;
        } elseif ($_SERVER['HTTP_X_PORT'] == "443") {
            $checkResult = TRUE;
        } else {
            $checkResult = FALSE;
        }
        
        if ($checkResult === TRUE) {
            header("Location: http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
            exit; 
        } else {
            return $checkResult;
        }
    } 
    
    
    public function checkMaintaice() {
        if (MAINTENANCE === true) {
            PageError::show503(MAINTENANCETIME);
            #PageError::show201(MAINTENANCETIME);
        }
    }
    
}

?>